<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="query"
        ref="queryForm"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="账号" prop="account">
          <common-input
            v-model="query.account"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="IP">
          <common-input v-model="query.ip" clearable @enter="handleQuery" />
        </el-form-item>
        <el-form-item label="公司名称">
          <common-input
            v-model="query.companyName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="责任人">
          <common-input
            v-model="query.principal"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->
    <el-row :gutter="10" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['apisecurity:applicationManage:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="!showBatch"
          @click="handleDelete"
          v-hasPermi="['alarmCenter:missRecord:remove']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>
    <!--列表 start-->
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="公司名称"
        prop="companyName"
        show-overflow-tooltip
      />

      <el-table-column
        label="账号"
        prop="account"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column
        label="IP"
        prop="ip"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="责任人"
        prop="principal"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column
        label="邮箱"
        prop="email"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="手机号"
        prop="phone"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column label="操作" align="left" width="120">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            @click="handleInfo(row)"
            v-hasPermi="['apisecurity:accountaudit:detail']"
            >详情</el-button
          >
          <el-button
            type="text"
            @click="handleEdit(row)"
            v-hasPermi="['apisecurity:accountaudit:detail']"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(row)"
            v-hasPermi="['apisecurity:accountaudit:detail']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <common-edit-dialog
      :title="title ? '新增合作方' : '编辑合作方'"
      :visible.sync="open"
      @cancel="cancel"
      @submit="submitForm"
      :buttonLoading="buttonLoading"
    >
      <el-form
        :model="form"
        label-width="100px"
        ref="form"
        :rules="rules"
        @submit.native.prevent
      >
        <el-row :gutter="18">
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <common-input v-model="form.companyName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号" prop="account">
              <common-input
                v-model="form.account"
                placeholder="请输入"
                :maxlength="32"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人" prop="principal">
              <common-input
                v-model="form.principal"
                placeholder="请输入"
                :maxlength="32"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP" prop="ip">
              <common-input
                v-model="form.ip"
                placeholder="请输入"
                style="width: calc(100% - 24px)"
              />
              <content-tip
                :tipType="'ip_segment'"
                :popoverWidth="'280'"
              ></content-tip>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <common-input v-model="form.email" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <common-input
                v-model="form.phone"
                placeholder="请输入"
                :maxLength="11"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="公司介绍" prop="companyIntroduction">
              <common-input
                v-model="form.companyIntroduction"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item> </el-col
          ><el-col :span="24">
            <el-form-item label="合作业务介绍" prop="businessIntroduction">
              <common-input
                v-model="form.businessIntroduction"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item> </el-col
          ><el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <common-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </common-edit-dialog>
    <common-info-dialog
      title="合作方详情"
      :visible.sync="infoOpen"
      @cancel="infoOpen = false"
    >
      <common-info-table>
        <common-info-table-cell label="公司名称">
          {{ info.companyName }}
        </common-info-table-cell>
        <common-info-table-cell label="账号">
          {{ info.account }}
        </common-info-table-cell>
        <common-info-table-cell label="责任人">
          {{ info.principal }}
        </common-info-table-cell>
        <common-info-table-cell label="IP">
          {{ info.ip }}
        </common-info-table-cell>
        <common-info-table-cell label="邮箱">
          {{ info.email }}
        </common-info-table-cell>
        <common-info-table-cell label="手机号">
          {{ info.phone }}
        </common-info-table-cell>
        <common-info-table-cell label="公司介绍">
          {{ info.companyIntroduction }}
        </common-info-table-cell>
        <common-info-table-cell label="合作业务介绍">
          {{ info.businessIntroduction }}
        </common-info-table-cell>
        <common-info-table-cell label="备注">
          {{ info.remark }}
        </common-info-table-cell>
      </common-info-table>
    </common-info-dialog>
  </common-list>
</template>

<script>
import BaseList from "@/mixins/BaseList";
import {
  getPageList,
  queryInfo,
  queryAdd,
  queryEdit,
  queryDelete,
} from "@/api/apisecurity/apipartner/index";
import ContentTip from "@/views/analysisCenter/log/ContentTip/index.vue";
export default {
  mixins: [BaseList],
  components: {
    ContentTip,
  },
  props: {},
  data() {
    const validateIPs = (rule, value, callback) => {
      if (value === "" || typeof value === "undefined" || value == null) {
        callback(new Error());
      } else {
        let values = value.split(",");
        const ipv4s_1 = new RegExp(
          "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])-(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$"
        );
        const ipv4s_2 = new RegExp(
          "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$"
        );
        const ipv6 = new RegExp(
          "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"
        );
        values.map((item) => {
          if (ipv4s_1.test(item) || ipv4s_2.test(item) || ipv6.test(item)) {
            callback();
          } else {
            callback(new Error());
          }
        });
      }
    };
    const validatePhoneNumber = (rule, value, callback) => {
      if (value) {
        const phoneNumber = new RegExp(
          "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$"
        );
        if (phoneNumber.test(value)) {
          callback();
        } else {
          callback(new Error());
        }
      } else {
        callback();
      }
    };
    return {
      info: {},
      infoOpen: false,
      buttonLoading: false,
      open: false,
      title: true,
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      form: {
        id: null,
        account: null,
        companyName: null,
        ip: null,
        principal: null,
        email: null,
        phone: null,
        principal: null,
        businessIntroduction: null,
        companyIntroduction: null,
        remark: null,
      },
      rules: {
        companyName: [
          {
            required: true,
            message: "请输入公司名称",
            trigger: "blur",
          },
        ],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        principal: [
          { required: true, message: "请输入责任人", trigger: "blur" },
        ],
        ip: [
          { required: true, message: "请输入IP", trigger: "blur" },
          {
            required: true,
            message: "IP格式错误，支持IPV4/IPV6格式",
            validator: validateIPs,
            trigger: "blur",
          },
        ],
        companyIntroduction: [
          { required: true, message: "请输入公司介绍", trigger: "blur" },
        ],
        businessIntroduction: [
          { required: true, message: "请输入合作业务介绍", trigger: "blur" },
        ],
        email: [
          { type: "email", message: "请输入正确的邮箱格式", trigger: "blur" },
        ],
        phone: [
          {
            message: "请输入正确的手机号",
            validator: validatePhoneNumber,
            trigger: "blur",
          },
        ],
      },
      query: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        companyName: null,
        ip: null,
        principal: null,
      }, // 查询条件
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      getPageList(this.query)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * 查询
     */
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
      this.date = [];
    },
    /**
     * 详情
     */
    handleInfo(row) {
      this.infoOpen = true;
      queryInfo({ id: row.id }).then((res) => {
        if (res && res.code === 200) {
          this.info = res.data;
        } else {
          this.info = {};
        }
      });
    },
    /**
     * 编辑
     */
    handleEdit(row) {
      this.title = false;
      this.open = true;
      Reflect.ownKeys(this.form).forEach((k) => (this.form[k] = row[k]));
    },
    /**
     * 新增
     */
    handleAdd() {
      this.title = true;
      this.open = true;
      this.form = {
        id: null,
        account: null,
        companyName: null,
        ip: null,
        principal: null,
        email: null,
        phone: null,
        principal: null,
        businessIntroduction: null,
        companyIntroduction: null,
        remark: null,
      };
    },
    /**
     * 删除
     */
    handleDelete(row) {
      const ids = row.id || this.ids.join(",");
      this.$GlobalDeleteConfirm().then(() => {
        queryDelete({ ids }).then((res) => {
          if (res && res.code == 200) {
            this.$GlobalSuccessMessage("删除成功");
            if (
              this.query.pageNum > 1 &&
              ids.split(",").length === this.list.length
            ) {
              this.query.pageNum -= 1;
            }
            this.getList();
          } else {
            this.$GlobalErrorConfirm("删除失败", res.msg);
          }
        });
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (!this.title) {
            queryEdit(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.cancel();
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            queryAdd(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.cancel();
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    cancel() {
      this.open = false;
    },
  },
};
</script>
